<?php
namespace app\common\library;
use think\Db;
/**
 * 后台管理的通用操作
 *
 * @author LCY <409028735@qq.com>
 */
trait Operate{
    /**
     * 通用的数据列表页
     * @return mixed|\think\response\Json
     * @throws \think\exception\DbException
     * Author: LCY<409028735@qq.com>
     * Time: 2021/12/2 17:17
     * Note:
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $page, $offset, $limit) = $this->buildSearchParams($this->searchFields);
            $rows = Db::name($this->tableName)->where($where)->order($sort, $order)->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
            $data = [
                'total' => $rows->total(),
                'rows' => $rows->items(),
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * 通用添加
     * @return mixed
     * Author: LCY<409028735@qq.com>
     * Time: 2021/11/30 10:47
     * Note:
     */
    public function add()
    {
        if ($this->request->isPost()) {
            //获取提交的数据
            $data = input('post.');
            //验证token

            //表单验证
            $this->validate_response($data, $this->modelName . '.add');

            //保存到数据库
            try {
                $res = model($this->modelName)->allowField(true)->save($data);
            } catch (\Exception $e) {
                $this->error('添加失败：' . $e->getMessage());
            }

            if (!$res) {
                $this->error('添加失败，请重试');
            }

            $this->success('添加完成');
        }

        return $this->fetch();
    }

    /**
     * 通用修改
     * @return mixed
     * Author: LCY<409028735@qq.com>
     * Time: 2021/11/30 10:56
     * Note:
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            //获取提交的数据
            $data = input('post.');
            //验证token

            //表单验证
            $this->validate_response($data, $this->modelName . '.edit');

            //保存到数据库
            try {
                $res = model($this->modelName)->allowField(true)->isUpdate(true)->save($data);
            } catch (\Exception $e) {
                $this->error('修改失败：' . $e->getMessage());
            }

            if (false === $res) {
                $this->error('修改失败，请重试');
            }

            $this->success('修改完成');
        }

        $id = input('param.id/d', 0);
        try {
            $info = model($this->modelName)->findOrEmpty($id);
        } catch (\Exception $e) {
            $this->error('修改失败：' . $e->getMessage());
        }

        if (empty($info)) {
            $this->error('数据不存在');
        }

        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 通用删除
     * Author: LCY<409028735@qq.com>
     * Time: 2021/11/30 10:59
     * Note:
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $ids = input('ids/s', '');
            if (empty($ids)) {
                $this->error('还未选择要删除的数据');
            }
            try {
                $res = model($this->modelName)->where('id', 'in', $ids)->delete();
            } catch (\Exception $e) {
                $this->error('修改失败：' . $e->getMessage());
            }

            if (!$res) {
                $this->error('删除失败，请重试');
            }

            $this->success('删除完成');
        }
    }

    /**
     * 查看数据详情页
     * @return mixed
     * Author: LCY<409028735@qq.com>
     * Time: 2021/12/2 16:33
     * Note:
     */
    public function detail()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            $this->error('查看的内容参数有误');
        }
        try {
            $res = model($this->modelName)->get($id);
        } catch (\Exception $e) {
            $this->error('查看失败：' . $e->getMessage());
        }

        if (!$res) {
            $this->error('查看的数据不存在');
        }

        $this->assign('info', $res);
        return $this->fetch();
    }

    /**
     * 通用的批量操作
     * Author: LCY<409028735@qq.com>
     * Time: 2021/11/30 11:06
     * Note:
     */
    public function multi()
    {
        if ($this->request->isPost()) {
            $ids = input('post.ids/s', 0);
            $field = input('post.field/s', '');
            $value = input('post.value/s', '');
            if (empty($ids) || empty($field)) {
                $this->error('参数有误');
            }

            try {
                $res = model($this->modelName)->where('id', 'in', $ids)->setField($field, $value);
            } catch (\Exception $e) {
                $this->error('设置失败：' . $e->getMessage());
            }

            if (false === $res) {
                $this->error('设置失败，请重试');
            }

            $this->success('设置完成');
        }
    }

    /**
     * 通用的切换设置
     * Author: LCY<409028735@qq.com>
     * Time: 2021/11/30 18:40
     * Note:
     */
    public function toggle()
    {
        if ($this->request->isPost()) {
            $ids = input('post.ids/d', 0);
            $field = input('post.field/s', '');
            $value = input('post.value/s', '');
            if (empty($ids) || empty($field)) {
                $this->error('参数有误');
            }

            try {
                $res = model($this->modelName)->where('id', 'in', $ids)->setField($field, $value);
            } catch (\Exception $e) {
                $this->error('设置失败：' . $e->getMessage());
            }

            if (false === $res) {
                $this->error('设置失败，请重试');
            }

            $this->success('设置完成');
        }
    }

    /**
     * 通用设置排序值
     * Author: LCY<409028735@qq.com>
     * Time: 2021/11/30 10:27
     * Note:
     */
    public function sort()
    {
        if ($this->request->isPost()) {
            $id = input('post.id/d', 0);
            $sort = input('post.sort/d', 0);
            if (empty($id)) {
                $this->error('ID参数有误');
            }
            if ($sort < 0 || $sort > 10000) {
                $this->error('排序设置范围：0 ~ 10000 之间');
            }
            try {
                $res = Db::name($this->tableName)->where('id', $id)->setField('sort', $sort);
            } catch (\Exception $e) {
                $this->error('修改失败：' . $e->getMessage());
            }

            $this->success('设置完成');
        }
    }

}